package com.cipe.cmrs.service;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.cipe.cmrs.model.ActionableAlert;
import com.cipe.cmrs.model.ActionableAlertsMessage;
import com.cipe.cmrs.model.Region;
import com.cipe.cmrs.model.User;
import com.cipe.cmrs.model.UserPreference;
import com.cipe.cmrs.util.Util;

/**
 * @author krishna The service class to Actionable Alerts message
 */
@Service
public class ActionableAlertMesageService extends
		BaseService<ActionableAlertsMessage> {
	/**
	 * This method is not going to use no more as it's return all the Actionable message.
	 * We can delete this one
	 * @return
	 */
	public List<ActionableAlertsMessage> getAllActionableAlert() {
		return this.dao
				.getResultList("SELECT c FROM ActionableAlertsMessage c "
						+ Util.createOrderByAsc("c.actionableAlert.type"));
	}

	/**
	 * Need to Sorted according to date.
	 * If the actionableAlert content null value then it's return all the ActionableAlertMessage.
	 * @param actionableAlert
	 * @return
	 */
	public List<ActionableAlertsMessage> getAllActionableAlert(
			UserPreference userPreference) {
		String datecondition = "";
		Calendar calendar = Calendar.getInstance();
		Timestamp timestamp = new Timestamp(new Date().getTime());
		if (userPreference.getAaDateid() != null) {
			if (2 == userPreference.getAaDateid()) {
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0); 
				datecondition = Util.createRangeQueryClause("c.date",
						new Timestamp(calendar.getTimeInMillis()), timestamp);
			} else if (3 == userPreference.getAaDateid()) {
				calendar.add(Calendar.DATE, -7);
				datecondition = Util.createRangeQueryClause("c.date",
						new Timestamp(calendar.getTimeInMillis()), timestamp);
			} else if (4 == userPreference.getAaDateid()) {
				calendar.add(Calendar.DATE, -30);
				datecondition = Util.createRangeQueryClause("c.date",
						new Timestamp(calendar.getTimeInMillis()), timestamp);
			}
		}
		String userCondition="";
		if(userPreference.getAaAsignId() !=null){
			userCondition=" and ( c.programOfficer="+userPreference.getAaAsignId()+" or c.regionaldirector="+userPreference.getAaAsignId()
			+" or c.programAssistant="+userPreference.getAaAsignId()+")";
		}
		String query = "SELECT c FROM ActionableAlertsMessage c where 1=1 and c.actionableAlert.id not in (7,8,12)"
				+ Util.createQueryClause("c.actionableAlert.id",
						userPreference.getAaid()) + datecondition+
						Util.createQueryClause("c.region",userPreference.getAaReagionId())+
						userCondition
				+ Util.createOrderByDesc("c.date");
		List<ActionableAlertsMessage> actionableAlertsMessages=this.dao.getResultList(query);
		/**
		 * For geeting the ActionabaleAlert related to Payment Request
		 * As payment have no reagion id 
		 */
		query = "SELECT c FROM ActionableAlertsMessage c where 1=1 and c.actionableAlert.id in (7,8)"
			+ Util.createQueryClause("c.actionableAlert.id",
					userPreference.getAaid()) + datecondition+
					userCondition
			+ Util.createOrderByDesc("c.date");
		actionableAlertsMessages.addAll(this.dao.getResultList(query));
		/**
		 * For geeting the ActionabaleAlert related Donoraward
		 * As Donoraward has no filter criteria.
		 */
		query = "SELECT c FROM ActionableAlertsMessage c where 1=1 and c.actionableAlert.id =12"
			+ Util.createQueryClause("c.actionableAlert.id",
					userPreference.getAaid()) +datecondition
			+ Util.createOrderByDesc("c.date");
		actionableAlertsMessages.addAll(this.dao.getResultList(query));
		return actionableAlertsMessages;
	}
}
